<?php
/**
 * User: zj
 * Date: 2017/4/18
 * Time: 10:53
 * Progress: 0%
 */

namespace app\index\controller;

use icontroller\admin\AController;
use icontroller\admin\index\IAdminMenu;
use logic\admin\index\LAdminMenu;
use think\Db;

class AdminMenu extends AController implements IAdminMenu
{
    private $logic;

    function __construct()
    {
        parent::__construct();
        $this->logic = new LAdminMenu();
    }

    public function index()
    {
        $data = $this->logic->index_data();
        $this->assign('list', $data);
        return view();
    }

    public function add()
    {
        $icon = Db::name('admin_menu_icon')->where(['id' => ['>', 361]])->column('icon');
        $this->assign('icon', json_encode($icon));
//        var_dump($icon);die();
        $parent_id = input('parent_id');
        if ($parent_id) {
            $item = $this->logic->edit_data($parent_id);
            $this->assign('item', $item);
        }
        return view();

    }

    public function insert()
    {
        $param = array(
            'parent_id' => input('parent_id'),
            'name' => input('name'),
            'sort' => input('sort'),
            'icon' => input('icon'),
            'url' => input('url'),
            'flag_use' => input('flag_use'),
        );
        return $this->logic->insert_data($param);

    }

    public function edit()
    {
        $icon = Db::name('admin_menu_icon')->where(['id' => ['>', 361]])->column('icon');
        $this->assign('icon', json_encode($icon));
        $id = input('id');
//        $data=Db::name('admin_menu m')
//            ->join('admin_menu c','m.parent_id=c.id','left')
//            ->join('admin_power p','p.id=m.power_id','left')
//            ->field(["m.*",'c.name as parent_name','p.id as power_id','concat(p.name,"(",p.module,"/",p.controller,"/",p.function,")")'=>'power_name'])->where(['m.id'=>$id])->find();;
        $data = $this->logic->edit_data($id);
//        var_dump($data);die();
        $this->assign('data', $data);
        return view();
    }

    public function update()
    {
        $id = input('id');
        $param = array(
            'name' => input('name'),
            'icon' => input('icon'),
            'parent_id' => input('parent_id'),
            'power_id' => input('power_id'),
            'url' => input('url'),
            'flag_use' => input('flag_use'),
            'sort' => input('sort')
        );
        return $this->logic->update_data($id, $param);
    }

    public function delete()
    {
        $id = input('id');
        return $this->logic->delete_data($id);
    }

    public function parent_tree_data()
    {
        $data = $this->logic->treeview_data();
        return $data;
    }

    public function power_tree_data()
    {
        $data = $this->logic->power_treeview();
        return $data;
    }

    public function set_power()
    {
        $id = input('id');
        if ($id) {
            $item = $this->logic->edit_data($id);
            $this->assign('data', $item);
        }

        $this->logic->power_treeview();
        return view();
    }

    public function set_power_update()
    {
        $id = input('id');
        $param = array(
            'power_id' => input('power_id')
        );
        return $this->logic->set_power($id, $param);
    }


}